package com.ddxcloud.movie.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddxcloud.common.constants.PayMethodConstant;
import com.ddxcloud.common.constants.PayTypeConstant;
import com.ddxcloud.common.constants.enums.PayStatusEnum;
import com.ddxcloud.common.handler.BusinessException;
import com.ddxcloud.common.utils.IpUtil;
import com.ddxcloud.common.utils.PayOrderUtil;
import com.ddxcloud.movie.config.common.CommonProperties;
import com.ddxcloud.movie.data.dto.PayDTO;
import com.ddxcloud.movie.data.entity.Order;
import com.ddxcloud.movie.data.entity.PaymentOrder;
import com.ddxcloud.movie.mapper.PaymentOrderMapper;
import com.ddxcloud.movie.service.OrderService;
import com.ddxcloud.movie.service.PaymentOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

@Service
@Slf4j
public class PaymentOrderServiceImpl extends ServiceImpl<PaymentOrderMapper, PaymentOrder> implements PaymentOrderService {

    @Resource
    OrderService orderService;

    @Resource
    CommonProperties commonProperties;

    @Override
    public PaymentOrder createPaymentOrder(PayDTO dto, HttpServletRequest request) {
        Order order = orderService.getById(dto.getOrderId());
        int payAmount = order.getOrderAmount().multiply(new BigDecimal(100)).intValue();
        if (order.getPreferentialAmount().compareTo(BigDecimal.ZERO) > 0) {
            payAmount = payAmount - order.getPreferentialAmount().multiply(new BigDecimal(100)).intValue();
        }
        // 订单并没有抵扣金额但应付金额小于等于0的情况下判断为异常
        if (order.getPreferentialAmount().compareTo(BigDecimal.ZERO) == 0 && payAmount <= 0) {
            throw new BusinessException("应付金额有误，请重新下单");
        }
        String outTradeNo = dto.getOrderId() + "_" + RandomUtil.randomNumbers(4);
        PaymentOrder paymentOrder = new PaymentOrder();
        paymentOrder.setOrderSn(PayOrderUtil.getOrderSn(PayTypeConstant.Payment));
        paymentOrder.setOrderId(dto.getOrderId());
        paymentOrder.setProductName(commonProperties.isDebug() ? "支付订单-测试" : "支付订单");
        paymentOrder.setPayMethod(dto.getPayMethod());
        paymentOrder.setPayChannel(dto.getPayChannel());
        paymentOrder.setPayAmount(commonProperties.isDebug() ? 1 : payAmount);
        paymentOrder.setPayStatus(PayStatusEnum.STATUS_0.getCode());
        paymentOrder.setCallbackUrl(commonProperties.getDomainName() + "/pay/" + PayMethodConstant.getPayMethod(dto.getPayMethod()) + "/notify");
        paymentOrder.setPlatform(order.getPlatform());
        paymentOrder.setClientIp(IpUtil.getClientIp(request));
        paymentOrder.setOutTradeNo(outTradeNo);
        log.info("创建支付订单-请求参数 {}", JSONUtil.toJsonStr(paymentOrder));
        if (this.save(paymentOrder)) {
            return paymentOrder;
        }
        throw new BusinessException("创建支付订单失败，请联系管理员");
    }

    @Override
    public Boolean updatePaymentOrderForPaySuccess(PaymentOrder paymentOrder) {
        LambdaUpdateWrapper<PaymentOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PaymentOrder::getPayStatus, PayStatusEnum.STATUS_1.getCode());
        updateWrapper.set(PaymentOrder::getPayTime, new Date());
        updateWrapper.eq(PaymentOrder::getId, paymentOrder.getId());
        return this.update(paymentOrder, updateWrapper);
    }
}
